package rolesystem.middle.managers;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import rolesystem.middle.managers.EnvironmentManager;
import rolesystem.middle.parameters.DefaultParametersFile;
import rolesystem.middle.utilityclasses.AggregateData;

public class DataOutputManager 
{
	private static boolean console;
	
	private static boolean screen;
	
	private static boolean file;
	
	private static long turn_counter;
	
	static public AggregateData dataSim;
	
	private static FileWriter dataFile;
	
	private static FileWriter aggregateDataFile;
	
	static	
	{
		DataOutputManager.console=true;
		DataOutputManager.screen=true;
		DataOutputManager.file=false;
		DataOutputManager.turn_counter=0;
		
		dataSim= new AggregateData();
		
		String workingDir = System.getProperty("user.dir");
		
		try {
			DataOutputManager.dataFile= new FileWriter (workingDir + File.separator + "test.txt");
			DataOutputManager.aggregateDataFile= new FileWriter (workingDir + File.separator + "aggregatedata.txt");
		} catch (IOException e) {
			aggregateDataFile=null;
			e.printStackTrace();		
		}	
	}
	
	public synchronized static void setOutputMode(boolean console, boolean screen, boolean file) 
	{
		DataOutputManager.console=console;
		DataOutputManager.screen=screen;
		DataOutputManager.file=file;	
	}
	
	public synchronized static void printString(String text) 
	{
		if(DataOutputManager.console==true)
			System.out.println(text);
		if(DataOutputManager.screen==true)
			EnvironmentManager.stateVisualization.append(text);
		if(DataOutputManager.file==true)
		{
			try {
				dataFile.append(text);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}			
	}
	
	public synchronized static void dataToFile(String text) 
	{
		try {
			dataFile.append(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String timestamp() 
	{
		Calendar calendar = Calendar.getInstance();
		Date now = calendar.getTime();
		
		return String.valueOf(now.getTime());  
	}
	
	public static long timestampLong() 
	{
		Calendar calendar = Calendar.getInstance();
		Date now = calendar.getTime();
		
		return now.getTime();  
	}
	
	public static long getSimulationStart() 
	{
		return dataSim.getStartTime();
	}
	
	public static void SimulationStart() 
	{
		dataSim.setStartTime(DataOutputManager.timestampLong());
	}
	
	public static void SimulationStop() 
	{
		dataSim.setEndTime(DataOutputManager.timestampLong());
	}
	
	public static void closeFile() 
	{
		try {
			dataFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public synchronized static void aggregateDataToFile() 
	{
		try {
			aggregateDataFile.append(dataSim.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	
	public synchronized static void periodicAggregateDataToFile() 
	{
		try {
			aggregateDataFile.append("Turn" + DefaultParametersFile.data_separator + turn_counter + DefaultParametersFile.row_separator + DefaultParametersFile.line_separator + dataSim.toStringPeriodic());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		turn_counter++;
	}
	
	public synchronized static void endsimulation() 
	{
		try {
			aggregateDataFile.append(DefaultParametersFile.line_separator+"ENDSIMULATION"
									 + DefaultParametersFile.row_separator + DefaultParametersFile.line_separator 
									 + DefaultParametersFile.line_separator + DefaultParametersFile.line_separator);
			aggregateDataFile.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		turn_counter=0;	
	}
	
	public synchronized static void closeAggregateDataFile() 
	{
		try {
			aggregateDataFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
